Let’s make some plotly plots
library(tidyverse)
library(httr)
library(jsonlite)
library(plotly)
get_all_inspections = function(url) {
all_inspections = vector("list", length = 0)
loop_index = 1
chunk_size = 50000
DO_NEXT = TRUE
while (DO_NEXT) {
message("Getting data, page ", loop_index)
all_inspections[[loop_index]] =
GET(url,
query = list(`$order` = "zipcode",
`$limit` = chunk_size,
`$offset` = as.integer((loop_index - 1) * chunk_size)
)
) %>%
content("text") %>%
fromJSON() %>%
as_tibble()
DO_NEXT = dim(all_inspections[[loop_index]])[1] == chunk_size
loop_index = loop_index + 1
}
all_inspections
}
url = "https://data.cityofnewyork.us/resource/43nn-pn8j.json"
nyc_inspections =
get_all_inspections(url) %>%
bind_rows()
# Select relevant columns and clean the data
cleaned_data <- nyc_inspections %>%
select(boro, inspection_date, critical_flag, latitude, longitude, grade, grade_date) %>%
filter(!is.na(boro) & !is.na(inspection_date) & !is.na(latitude) & !is.na(longitude) & !is.na(grade)) %>%
mutate(inspection_date = ymd(inspection_date),
grade_date = ymd(grade_date))
# Count of grades per borough
grade_boro_count <- cleaned_data %>%
group_by(boro, grade) %>%
summarise(count = n()) %>%
ungroup()
# Prepare data for heatmap
heatmap_data <- cleaned_data %>%
group_by(boro, grade) %>%
summarise(count = n()) %>%
ungroup()
plot_ly(grade_boro_count, x = ~boro, y = ~count, color = ~grade, type = "bar") %>%
layout(title = "Distribution of Inspection Grades by Borough",
xaxis = list(title = "Borough"),
yaxis = list(title = "Count"))
plot_ly(cleaned_data, x = ~longitude, y = ~latitude, type = 'scatter', mode = 'markers',
color = ~grade, text = ~paste("Grade:", grade, "<br>Borough:", boro)) %>%
layout(title = "Inspection Locations by Grade",
xaxis = list(title = "Longitude"),
yaxis = list(title = "Latitude"))
plot_ly(heatmap_data, x = ~boro, y = ~grade, z = ~count, type = "heatmap", color = "blue") %>%
layout(title = "Heatmap of Grade Distribution by Borough",
xaxis = list(title = "Borough"),
yaxis = list(title = "Grade"))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels